package com.e3.core.base;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Date;

import com.e3.web.core.model.SuperModel;

public class ServiceInvoker implements InvocationHandler {
	
	private Object service;
	
	public ServiceInvoker(){
		
	}
	
	public ServiceInvoker(Object service){
		this.service = service;
	}
	
	public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {
		if(arguments!=null&&arguments.length>0){
			for (Object object : arguments) {
				if(object!=null&&SuperModel.class.isAssignableFrom(object.getClass())){
					SuperModel model = (SuperModel)object;
					Date createTime = model.getCreateTime();
					Date currentDate = new Date();
					if(createTime == null){
						model.setCreateTime(currentDate);
					}
					Long userId = model.getCreateUserId();
					if(userId == null||userId == 0){
						model.setCreateUserId(SessionContext.getUserID());
					}
					model.setModifyTime(currentDate);
					model.setModifyUserId(SessionContext.getUserID());
				}
			}
		}
		
		return method.invoke(service, arguments);
	}

}
